package com.masterclient.app.businessmodule.settingmodule.auth.contract;

import com.eoner.baselib.container.CommonBaseResponse;
import com.eoner.common.bean.base.CommonBaseBean;
import com.masterclient.app.businessmodule.settingmodule.api.SettingHttpService;
import com.masterclient.app.businessmodule.settingmodule.auth.bean.OcrBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;


public class UploadIDsPresenter extends UploadIDsContract.Presenter{

    @Override
    public void new_ocr(File image, String type) {
        mView.baseShowCommitLoading("正在上传...");
        Map<String, RequestBody> map = new HashMap<>();
        RequestBody requestBody1 =  RequestBody.create(MediaType.parse("multipart/form-data"), image);

        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);//表单类型

        RequestBody imageBody = RequestBody.create(MediaType.parse("image/jpeg"), image);
        builder.addFormDataPart("image", "123.jpeg", imageBody);//imgfile 后台接收图片流的参数名
        builder.addFormDataPart("type",type);

        Observable<CommonBaseResponse<OcrBean>> observable = SettingHttpService.getInstance().new_ocr(builder.build().parts());
        Disposable disposable = toSubscribe(observable, new Consumer<CommonBaseResponse<OcrBean>>() {
            @Override
            public void accept(@NonNull CommonBaseResponse<OcrBean> bean) throws Exception {
                mView.baseHiddenCommitLoading();
                if(0 == bean.getCode()){
                    mView.OnNewOcrResult(bean.getData());
                }else{
                    mView.OnNewOcrResultFail(bean.getMsg());
                }
            }
        },  new Consumer<Throwable>() {
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                o.printStackTrace();
                mView.baseHiddenCommitLoading();
                mView.OnNewOcrResultFail(o.getMessage());
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void ocr_info_send( String real_name) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("real_name",real_name);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        mRxManager.add(toSubscribe(SettingHttpService.getInstance().ocr_info_send(body), new Consumer<CommonBaseBean>() {
            @Override
            public void accept(CommonBaseBean bean) throws Exception {
                if ("0".equals(bean.getCode())){
                    mView.onIDOcrSuccess();
                }else {
                    mView.OnIDOcrFail(bean.getCode(),bean.getMsg());
                }
            }
        }, (Consumer<Throwable>) o -> {
            o.printStackTrace();
            mView.OnIDOcrFail("4","网络异常，请重试");
        }));
    }

}